// ==UserScript==
// @name	百度网盘toAria2
// @author	Mormts
// @description	快速添加百度网盘文件到Aria2。
// @encoding	utf-8
// @include     http://pan.baidu.com/share/link*
// @include     http://yun.baidu.com/share/link*
// @run-at	document-end
// @version	0.1.5
// ==/UserScript==

var jstxt = '';
jstxt += 'function start() {'+'\n';
jstxt += '	if (button_click_state == 0){'+'\n';
jstxt += '		if(disk.util.ViewShareUtils){'+'\n';
jstxt += '			this.fileitem = JSON.parse(disk.util.ViewShareUtils.viewShareData);'+'\n';
jstxt += '			this.aria2_obj = combination(this.fileitem["dlink"],this.fileitem["path"]);'+'\n';
jstxt += '			postaria2obj(this.aria2_obj);'+'\n';
jstxt += '		}'+'\n';
jstxt += '		else {'+'\n';
jstxt += '			this.fileitem = FileUtils.getListViewCheckedItems();'+'\n';
jstxt += '			if (this.fileitem.length == 0){SetMessageRect("请先选择1个以上的文件",5000,"caution");return null}'+'\n';
jstxt += '			else{'+'\n';
jstxt += '				for(var i=0;i<this.fileitem.length;i++){'+'\n';
jstxt += '					if (this.fileitem[i].md5) {'+'\n';
jstxt += '						this.aria2_obj = combination(this.fileitem[i].dlink,this.fileitem[i].path);'+'\n';
jstxt += '						postaria2obj(this.aria2_obj);'+'\n';
jstxt += '					}else{var folder_state = 1}'+'\n';
jstxt += '				}'+'\n';
jstxt += '			}'+'\n';
jstxt += '		}'+'\n';
jstxt += '		if(folder_state == 1){SetMessageRect("暂不支持文件夹打包下载，待更新。文件已添加",5000,"caution");}'+'\n';
jstxt += '		else{SetMessageRect("已添加....到WEB界面查看确认",5000,"ok");}'+'\n';
jstxt += '	}'+'\n';
jstxt += '}'+'\n';

jstxt += 'function SetMessageRect(txt_,time_,type) {'+'\n';
jstxt += '	button_click_state = 1;'+'\n';

jstxt += '	if(type == "caution"){type_ = "sprite-ic b-in-blk b-ic-dimen-1 ic-mini-caution"}'+'\n';
jstxt += '	else{type_ = "sprite-ic b-in-blk b-ic-dimen-1 ic-mini-ok"}'+'\n';

jstxt += '	this.DivMessage = getboxclass("div","b-panel b-dialog toast-dialog toast-content b-bdr-6 bdr-rnd-3 box-shadow");'+'\n';
jstxt += 'if (! this.DivMessage){'+'\n';
jstxt += '	var AddMessageDiv = document.createElement("div");'+'\n';
jstxt += '	AddMessageDiv.className = "b-panel b-dialog toast-dialog toast-content b-bdr-6 bdr-rnd-3 box-shadow";'+'\n';
jstxt += '	AddMessageDiv.style.display = "none";'+'\n';
jstxt += '	AddMessageDiv.innerHTML = \'<div class="toast-outer"><div id="_disk_id_2" class="toast-msg ellipsis"><em class="b-in-blk b-ic-dimen-1 loading"></em></div>\';'+'\n';
jstxt += '	document.getElementsByTagName("body").item(0).appendChild(AddMessageDiv);'+'\n';
jstxt += '	this.DivMessage = AddMessageDiv;'+'\n';
jstxt += '}'+'\n';

jstxt += '	getboxclass("div","toast-msg ellipsis").innerHTML = \'<em class="\' + type_ + \'"></em>\' + txt_ + \'<em class="close-tips" style="display: none;"> </em>\';'+'\n';
jstxt += '	this.DivMessage.style.display = "block";'+'\n';
//jstxt += '	this.ParentDIV = getboxclass("div","link-title clearfix btn-list");'+'\n';
//jstxt += '	if(! this.ParentDIV){this.ParentDIV = getboxclass("section","bd bdy b-rlv clearfix");}'+'\n';
//jstxt += '	this.left_ = String((this.ParentDIV.scrollWidth - this.DivMessage.scrollWidth) / 2);'+'\n';
//jstxt += '	this.top_ = String(this.ParentDIV.getBoundingClientRect().top + 5);'+'\n';
jstxt += '	if(getboxclass("section","bd b-rlv clearfix")){this.ParentDIV_top = getboxclass("section","bd b-rlv clearfix");this.ParentDIV_left = getboxclass("section","flag2-fns");}'+'\n';
jstxt += '	else if(getboxclass("section","bd bdy b-rlv clearfix")){this.ParentDIV_top = getboxclass("section","bd bdy b-rlv clearfix");this.ParentDIV_left=this.ParentDIV_top}'+'\n';

jstxt += '	this.left_ = String((this.ParentDIV_left.scrollWidth - this.DivMessage.scrollWidth) / 2);'+'\n';
jstxt += '	this.top_ = String(this.ParentDIV_top.offsetTop + 25);'+'\n';
jstxt += '	this.DivMessage.style.left = this.left_ + "px";'+'\n';
jstxt += '	this.DivMessage.style.top = this.top_ + "px";'+'\n';
jstxt += '	this.timeout=function(){this.DivMessage.style.display = "none";button_click_state = 0;}'+'\n';
jstxt += '	setTimeout(this.timeout, parseInt(time_))'+'\n';
jstxt += '}'+'\n';

jstxt += 'function postaria2obj(addobj) {'+'\n';
jstxt += '	button_click_state = 1;'+'\n';
jstxt += '	var httppost = new XMLHttpRequest();'+'\n';
jstxt += '	this.jsonrpc_path = GetCookie("ARIA2JSONRPC");'+'\n';
jstxt += '	httppost.open("POST", this.jsonrpc_path+"?tm="+(new Date()).getTime().toString(), true);'+'\n';
jstxt += '	httppost.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");'+'\n';
jstxt += '	httppost.send(JSON.stringify(addobj));'+'\n';
//jstxt += '	return JSON.stringify(addobj);'+'\n';
jstxt += '	button_click_state = 0;'+'\n';
jstxt += '}'+'\n';

jstxt += 'function combination(url,name) {'+'\n';
jstxt += '	var post_obj = [{'+'\n';
jstxt += '		"jsonrpc": "2.0",'+'\n';
jstxt += '		"method": "aria2.addUri",'+'\n';
jstxt += '		"id": (new Date()).getTime().toString(),'+'\n';
jstxt += '		"params": [[url],{'+'\n';
jstxt += '			"out":name,'+'\n';
jstxt += '			"split":"5",'+'\n';
jstxt += '			"max-connection-per-server":"5",'+'\n';
jstxt += '			"seed-ratio":"1.0"}'+'\n';
jstxt += '		]'+'\n';
jstxt += '	}];'+'\n';
jstxt += '	return post_obj'+'\n';
jstxt += '}'+'\n';

jstxt += 'function bind(obj,type,fn) {'+'\n';
jstxt += '    if (obj.attachEvent) {'+'\n';
jstxt += '        obj["e"+type+fn] = fn;'+'\n';
jstxt += '        obj[type+fn] = function(){obj["e"+type+fn](window.event);}'+'\n';
jstxt += '        obj.attachEvent("on"+type,obj[type+fn]);'+'\n';
jstxt += '    } else {'+'\n';
jstxt += '        obj.addEventListener(type,fn,false);}'+'\n';
jstxt += '}'+'\n';


jstxt += 'function SetCookie(name,value)'+'\n';
jstxt += '{  '+'\n';
jstxt += '    var Days = 365;'+'\n';
jstxt += '    var exp  = new Date();'+'\n';
jstxt += '   exp.setTime(exp.getTime() + Days*24*60*60*1000);  '+'\n';
jstxt += '    document.cookie = name + "="+ escape (value) + ";expires=" +exp.toGMTString()+";path=/;"; '+'\n';
jstxt += '} '+'\n';

jstxt += 'function GetCookie(name)'+'\n';
jstxt += '{ '+'\n';
jstxt += 'var arr = document.cookie.match(new RegExp("(^| )"+name+'+'\n';
jstxt += '"=([^;]*)(;|$)")); '+'\n';
jstxt += '        if(arr != null) return unescape(arr[2]); return null; '+'\n';
jstxt += '} '+'\n';

jstxt += 'function SetMessage() {'+'\n';
jstxt += '	this.jsonrpc_path = GetCookie("ARIA2JSONRPC");'+'\n';
jstxt += '	var inttxt = prompt("请输入Aria2 JSON-RPC地址",this.jsonrpc_path);'+'\n';
jstxt += '	if(inttxt){SetCookie("ARIA2JSONRPC",inttxt);};'+'\n';
jstxt += '}'+'\n';

jstxt += 'function getboxclass(type,classname) {'+'\n';
jstxt += '	this.getclassitem = document.getElementsByTagName(type);'+'\n';
jstxt += '	for(var i=0;i<this.getclassitem.length;i++){'+'\n';
jstxt += '		if(this.getclassitem[i].className == classname){'+'\n';
jstxt += '			return this.getclassitem[i];'+'\n';
jstxt += '				}}};'+'\n';

jstxt += 'function addaria2button() {'+'\n';
jstxt += '	this.className_ = "new-dbtn";'+'\n';
jstxt += '	if (getboxclass("span", "btn-list")) {this.place = getboxclass("span", "btn-list");}'+'\n';
jstxt += '	else if (getboxclass("div", "bottomBtnBar")) {this.place = getboxclass("div", "bottomBtnBar");}'+'\n';
jstxt += '	else if (document.getElementById("downFileButtom")) {this.place = document.getElementById("downFileButtom").parentNode;}'+'\n';
jstxt += '	else if (getboxclass("div", "link-title clearfix btn-list")) {'+'\n';
jstxt += '		if(! document.getElementById("cancelButton")){this.place = getboxclass("div", "link-title clearfix btn-list");}'+'\n';
jstxt += '		else {'+'\n';
jstxt += '			this.className_ = "new-dbtn cancel b-fr b-fr";'+'\n';
jstxt += '			this.place = document.getElementById("cancelButton").parentNode;'+'\n';
jstxt += '		}}'+'\n';
//jstxt += '	else if(document.getElementById("cancelButton")) {'+'\n';
//jstxt += '		this.className_ = "new-dbtn cancel b-fr b-fr";'+'\n';
//jstxt += '		this.place = document.getElementById("cancelButton").parentNode;'+'\n';
//jstxt += '	}'+'\n';
//jstxt += '	else {'+'\n';
//jstxt += '		this.className_ = "new-dbtn cancel b-fr b-fr";'+'\n';
//jstxt += '		this.place = getboxclass("div", "link-title clearfix btn-list");'+'\n';
//jstxt += '	}'+'\n';

jstxt += '	this.A = document.createElement("a");'+'\n';
jstxt += '	this.A.className = this.className_;'+'\n';
jstxt += '	this.A.id = "aria2button";'+'\n';
jstxt += '	this.A.herf = "javascript:;";'+'\n';
jstxt += '	this.A.innerHTML = \'<em class="icon-download"></em><b>添加到Aria2</b>\';'+'\n';
jstxt += '	this.place.insertBefore(this.A, this.place.childNodes[0]);'+'\n';
jstxt += '	}'+'\n';

jstxt += 'function addconfigbutton() {'+'\n';
jstxt += '	this.span_ = document.createElement("span");'+'\n';
jstxt += '	this.span_.title = "aria2config";'+'\n';
jstxt += '	this.span_.innerHTML = \'<a id="setconfig_aria2" href="javascript:;">配置Aria2</a>\';'+'\n';

jstxt += '	if (getboxclass("div", "sh-cnt-ct")) {this.span_.className = "ellipsis"; getboxclass("div", "sh-cnt-ct").appendChild(this.span_);}'+'\n';
jstxt += '	else if (getboxclass("div", "sh-cnt-ct b-fr")){this.place=getboxclass("div", "sh-cnt-ct b-fr"); this.span_.className = "abuse"; this.place.insertBefore(this.span_, this.place.childNodes[0]);}'+'\n';
jstxt += '		 else {'+'\n';
jstxt += '				SetMessageRect("由于兼容问题无法显示配置选项...",6000,"caution");'+'\n';
jstxt += '				button_click_state = 1;'+'\n';
jstxt += '		}}'+'\n';

jstxt += 'if (! GetCookie("ARIA2JSONRPC")){SetCookie("ARIA2JSONRPC","http://192.168.1.1:6800/jsonrpc");};'+'\n';
jstxt += 'addconfigbutton();'+'\n';
jstxt += 'addaria2button();'+'\n';

jstxt += 'bind(document.getElementById("setconfig_aria2"), "click", SetMessage);'+'\n';
jstxt += 'bind(document.getElementById("aria2button"), "click", start);'+'\n';
jstxt += 'var button_click_state = 0;'+'\n';


var script_ = document.createElement("script");
script_.type = "text/javascript"; 
script_.innerHTML = jstxt;
var head_ = document.getElementsByTagName("head").item(0);
head_.appendChild(script_);
